



import bpy
import numpy as np

import 全局控制.Node.节点和插座基类.bl_节点基类 as 基类
import 全局控制.Node.插座.bl_插座基类 as 插座基类
import Core.bl_UI配色 as UI配色

import engine.initLIB.load_dll as engineLib
import ctypes




engineLib.flexCAPI.f_创建标量计算节点.restype = ctypes.c_bool
engineLib.flexCAPI.f_创建矩阵与Vec3计算节点.restype = ctypes.c_bool
engineLib.flexCAPI.f_创建矩阵与Vec4计算节点.restype = ctypes.c_bool






E_类型枚举 = [
	("TYPE_加法", "+", "", 0),
	("TYPE_减法", "-", "", 1),
	("TYPE_乘法", "*", "", 2),
	("TYPE_除法", "/", "", 3),
	("TYPE_取余", "%", "", 4),

	("TYPE_取反", "！", "", 5),
	("TYPE_取负", "取负", "", 6),
	("TYPE_左移", "<<", "", 7),
	("TYPE_右移", ">>", "", 8),

	("TYPE_累加", "+=", "", 9),
	("TYPE_累减", "-=", "", 10),

	("TYPE_按位与", "&", "&", 11),
	("TYPE_按位或", "|", "|", 12),
	("TYPE_按异或", "^", "^", 13),

	("TYPE_平方", "**", "**", 14),
]




E_运算符类型枚举 = [
	("TYPE_加法", "+", "", 0),
	("TYPE_减法", "-", "", 1),
	("TYPE_乘法", "*", "", 2),
	("TYPE_除法", "/", "", 3),
	("TYPE_取余", "%", "", 4),

	("TYPE_累加", "+=", "", 5),
	("TYPE_累减", "-=", "", 6),
	("TYPE_累乘", "*=", "", 7),
	("TYPE_累除", "/=", "", 8),

]

E_矢量运算符类型枚举 = [
	("TYPE_加法", "+", "", 0),
	("TYPE_减法", "-", "", 1),
	("TYPE_乘法", "*", "", 2),
	("TYPE_除法", "/", "", 3),
	("TYPE_点积", "dot", "", 4),
	("TYPE_差积", "coss", "", 5),
]



























def f_设置计算类型(self, context):
	data = list(str(self.m_运算类型))
	engineLib.flexCAPI.f_node_传入BL数据(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), ctypes.py_object(data));
	if context :
		self.id_data.f_节点数据更新_更新树([self])

class C_Node_标量运算(基类.C_Node_基类):
	bl_dname = 'bl_node_标量运算'
	bl_label = '标量运算'
	
	m_运算类型 = bpy.props.EnumProperty(items = E_运算符类型枚举, name="运算", default="TYPE_加法", update=f_设置计算类型)
	def init(self, context):
		super(C_Node_标量运算, self).f_init(context)
		self.inputs.new('C_Socket_F32_Array1D', name='A')
		self.inputs.new('C_Socket_F32_Array1D', name='B')

		self.inputs.new('C_Socket_I32_Array1D', name='索引A')
		self.inputs.new('C_Socket_I32_Array1D', name='索引B')

		self.outputs.new('C_Socket_F32_Array1D', name='C')
		self.f_toCPP(self.id_data)

	def draw_buttons(self, context, layout):
		#row = layout.row()
		layout.prop(self, 'm_运算类型')
		#layout.prop(self, 'm_输出插座类型', icon='PLUGIN')
	def f_toCPP(self, tree):
		是否更新 = engineLib.flexCAPI.f_创建标量计算节点(ctypes.c_wchar_p(tree.name), ctypes.c_wchar_p(self.name), self.f_getPyObject(tree));
		if 是否更新 :
			f_设置计算类型(self, None)
			self.inputs['A'].f_update()
			self.inputs['B'].f_update()
			

class C_MENU_Add_标量运算节点(bpy.types.Operator):
	bl_idname = 'node.global_add_scalar_operation_node'
	bl_label = '标量运算'
	#bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_标量运算', use_transform=True)
		return {'FINISHED'}



















class C_Node_矩阵运算(基类.C_Node_基类):
	bl_dname = 'bl_node_矩阵运算'
	bl_label = '矩阵运算'

	m_运算类型 = bpy.props.EnumProperty(items = E_类型枚举, name="矩阵运算", default="TYPE_乘法", update=f_设置计算类型 )
	def init(self, context):
		super(C_Node_矩阵运算, self).f_init(context)
		self.inputs.new('C_Socket_矩阵4X4_Array1D', name='矩阵A')
		self.inputs.new('C_Socket_矩阵4X4_Array1D', name='矩阵B')
		self.inputs.new('C_Socket_I32_Array1D', name="A索引")
		self.inputs.new('C_Socket_I32_Array1D', name="B索引")

		self.outputs.new('C_Socket_矩阵4X4_Array1D', name='矩阵C')

		self.use_custom_color = True
		self.color = UI配色.g_内部节点颜色
		self.f_toCPP(self.id_data)
		
	def draw_buttons(self, context, layout):
		layout.prop(self, 'm_运算类型')

	def f_toCPP(self, tree):
		是否更新 = engineLib.flexCAPI.f_创建矩阵计算节点(ctypes.c_wchar_p(tree.name), ctypes.c_wchar_p(self.name), self.f_getPyObject(tree));
		if 是否更新 :
			f_设置计算类型(self, None)
			self.inputs['矩阵A'].f_update()
			self.inputs['矩阵B'].f_update()
			self.inputs['A索引'].f_update()
			self.inputs['B索引'].f_update()
			self.outputs['矩阵C'].f_update()

		


class C_NODE_AddMenu_矩阵运算(bpy.types.Operator):
	bl_idname = 'node.global_add_materix_operation'
	bl_label = '矩阵运算'
	#bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_矩阵运算', use_transform=True)
		return {'FINISHED'}













class C_Node_矩阵与Vec3运算(基类.C_Node_基类):
	bl_dname = 'bl_node_矩阵与Vec3运算'
	bl_label = '矩阵与Vec3运算'

	m_运算类型 = bpy.props.EnumProperty(items = E_类型枚举, name="矩阵@Vec3", default="TYPE_乘法", update=f_设置计算类型 )
	def init(self, context):
		super(C_Node_矩阵与Vec3运算, self).f_init(context)
		self.inputs.new('C_Socket_Vec3_Array1D', name='矢量')
		self.inputs.new('C_Socket_矩阵4X4_Array1D', name='矩阵')
		self.outputs.new('C_Socket_Vec3_Array1D', name='结果')
		self.f_toCPP(self.id_data)
		self.use_custom_color = True
		self.color = UI配色.g_内部节点颜色
		
	def draw_buttons(self, context, layout):
		layout.prop(self, 'm_运算类型')

	def f_toCPP(self, tree):
		是否更新 = engineLib.flexCAPI.f_创建矩阵与Vec3计算节点(ctypes.c_wchar_p(tree.name), ctypes.c_wchar_p(self.name), self.f_getPyObject(tree));
		if 是否更新 :
			f_设置计算类型(self, None)
			self.inputs['矩阵'].f_update()
			self.inputs['矢量'].f_update()
			self.outputs['结果'].f_update()

class C_NODE_AddMenu_矩阵与Vec3运算(bpy.types.Operator):
	bl_idname = 'node.global_add_materixff_c_vect_operation'
	bl_label = '矩阵与Vec3运算'
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_矩阵与Vec3运算', use_transform=True)
		return {'FINISHED'}






class C_Node_矩阵与Vec4运算(基类.C_Node_基类):
	bl_dname = 'bl_node_矩阵与Vec3运算'
	bl_label = '矩阵与Vec4运算'

	m_运算类型 = bpy.props.EnumProperty(items = E_类型枚举, name="矩阵@Vec4", default="TYPE_乘法", update=f_设置计算类型 )
	def init(self, context):
		super(C_Node_矩阵与Vec4运算, self).f_init(context)
		self.inputs.new('C_Socket_Vec4_Array1D', name='矢量')
		self.inputs.new('C_Socket_矩阵4X4_Array1D', name='矩阵')
		self.outputs.new('C_Socket_Vec4_Array1D', name='结果')
		self.f_toCPP(self.id_data)
		self.use_custom_color = True
		self.color = UI配色.g_内部节点颜色
		
	def draw_buttons(self, context, layout):
		layout.prop(self, 'm_运算类型')
	def f_toCPP(self, tree):
		是否更新 = engineLib.flexCAPI.f_创建矩阵与Vec4计算节点(ctypes.c_wchar_p(tree.name), ctypes.c_wchar_p(self.name), self.f_getPyObject(tree));
		if 是否更新 :
			f_设置计算类型(self, None)
			self.inputs['矩阵'].f_update()
			self.inputs['矢量'].f_update()
			self.outputs['结果'].f_update()
		
class C_NODE_AddMenu_矩阵与Vec4运算(bpy.types.Operator):
	bl_idname = 'node.global_add_materixff_c_vecf_operation'
	bl_label = '矩阵与Vec4运算'
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_矩阵与Vec4运算', use_transform=True)
		return {'FINISHED'}













classes = (
	
	C_Node_标量运算,
	C_MENU_Add_标量运算节点,
	
	C_Node_矩阵运算,
	C_NODE_AddMenu_矩阵运算,

	C_Node_矩阵与Vec3运算,
	C_NODE_AddMenu_矩阵与Vec3运算,
	C_Node_矩阵与Vec4运算,
	C_NODE_AddMenu_矩阵与Vec4运算,

)




def register():
	for c in classes:
		bpy.utils.register_class(c)
		

def unregister():
	for c in classes:
		bpy.utils.unregister_class(c)

	

